﻿using Architecture.Repository.Interface.Traceability;
using Traceability.Domain.SlaughterDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.SlaughterServices
{
    public class SlaughterService :ISlaughterService
    {
        /// <summary>
        /// 屠宰仓储
        /// </summary>
        protected readonly ISlaughterReposiotry slaughterReposiotry;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="slaughterReposiotry">屠宰仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public SlaughterService(ISlaughterReposiotry slaughterReposiotry, IPictureRepository pictureRepository)
        {
            this.slaughterReposiotry = slaughterReposiotry;
            this.pictureRepository = pictureRepository;
        }

        public async Task<int> AddSlaughter(Slaughter slaughter)
        {
            return await slaughterReposiotry.AddAsync(slaughter);
        }

        public async Task<List<SlaughterDto>> GetAllSlaughterList(int shoppingId)
        {
            var list = (from a in await slaughterReposiotry.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.SlaughterId equals b.Slaughter
                        where !a.IsDeleted && a.ShoppingId == shoppingId
                        select new SlaughterDto
                        {
                            SlaughterId = a.SlaughterId,
                            Batch = a.Batch,
                            AveWeight = a.AveWeight,
                            ArriveDate = a.ArriveDate,
                            SlaughterTime = a.SlaughterTime,
                            ShoppingId = a.ShoppingId,
                            CreatedBy = a.CreatedBy,
                            CreatedDate = a.CreatedDate,
                            UpdatedBy = a.UpdatedBy,
                            UpdatedDate = a.UpdatedDate,
                            DeletedBy = a.DeletedBy,
                            DeletedDate = a.DeletedDate,
                            IsDeleted = a.IsDeleted,
                            PictureId = b.PictureId,
                            PictureName = b.PictureName,
                        }).Distinct().ToList();
            return list;
        }

        public async Task<int> UpdateSlaughter(Slaughter slaughter)
        {
            return await slaughterReposiotry.UpdateAsync(slaughter);
        }

        public async Task<int> DeleteSlaughters(int slaughterId, string userMame)
        {
            var res = await slaughterReposiotry.GetModelAsync(slaughterId);
            res.IsDeleted = true;
            res.DeletedBy = userMame;
            res.DeletedDate = DateTime.Now;
            return await slaughterReposiotry.UpdateAsync(res);
        }

        public async Task<Slaughter> GetSlaughterModel(int slaughterId)
        {
            return await slaughterReposiotry.GetModelAsync(slaughterId);
        }
    }
}
